package com.lora.service.resolver;

import com.google.common.base.Strings;
import com.lora.dto.EquipmentDto;
import com.lora.entity.Infraredinduction;
import com.lora.service.InfraredinductionService;
import com.lora.service.NodeDataResolver;
import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service("infraredinductionResolver")
public class InfraredinductionResolver implements NodeDataResolver<Infraredinduction> {

    @Resource
    private InfraredinductionService infraredinductionService;

    /**
     *
     * @param equipmentNumber
     * @param data
     * @return
     *
     * 占用5个字节。第1个字节客户ID，固定为02；第2个字节为设备类型，01为红外；第3个字节为帧类型，00代表心跳包，01代表业务数据；
     * 第4个字节 bit7-6传感器状态 00代表正常，01代表告警，10代表故障，11代表预热；bit5-4电池状态 00代表正常，01代表电池低电量；
     * bit3防拆状态，0代表正常，1代表被拆；bit2-0按键状态(暂未实现)，000代表正常，001代表按键自检；
     * 第5个字节是电压值,电池电压上报值为实际电池电压的10倍，比如电池电压为3.6V，则上报值为0x24
     *
     * 示例：红外报警器上传的数据为 AgEBQCU= 解码并转为16进制是02 01 01 40 25 ，第1个字节固定为02，第2个字节01为红外，
     * 第3个字节01代表业务数据，第4个字节40转换成二进制01000000，第6-7位为01即代表告警，第4-5位00为电池状态00代表正常，
     * 第3位是0即正常状态，第0-2位为按键状态保留位，最后一个字节25为电压值，转为10进制除非10即3.7V。
     *
     */
    @Override
    public EquipmentDto<Infraredinduction> resolve(String equipmentNumber, String data) {
        EquipmentDto<Infraredinduction> equipmentData = new EquipmentDto<>();
        byte[] sources = Base64Utils.decodeFromString(data);
        String b1ToHex = Strings.padStart(Integer.toHexString(sources[0] & 0xff), 2, '0');
        String b2ToHex = Strings.padStart(Integer.toHexString(sources[1] & 0xff), 2, '0');
        String b3ToHex = Strings.padStart(Integer.toHexString(sources[2] & 0xff), 2, '0');
        String b4ToHex = Strings.padStart(Integer.toHexString(sources[3] & 0xff), 2, '0');
        String b5ToHex = Strings.padStart(Integer.toHexString(sources[4] & 0xff), 2, '0');
        if("02".equals(b1ToHex) && "01".equals(b2ToHex) && "01".equals(b3ToHex)) {
            String busiBinary = Strings.padStart(Integer.toBinaryString(sources[3] & 0xff), 8, '0');
            String sensorStatus = busiBinary.substring(0, 2);
            String batteryStatus = busiBinary.substring(2, 4);
            String tamperStatus = busiBinary.substring(4, 5);
            String keyStatus = busiBinary.substring(5, 8);
            Infraredinduction infraredinduction = new Infraredinduction();
            infraredinduction.setEquipmentid(equipmentNumber);
            infraredinduction.setDatastring(data);
            infraredinduction.setState(sensorStatus);
            infraredinduction.setBatterystate(batteryStatus);
            infraredinduction.setTamperstate(tamperStatus);
            infraredinduction.setKeystate(keyStatus);
            // 第5个字节是电压值,电池电压上报值为实际电池电压的10倍，比如电池电压为3.6V，则上报值为0x24
            // 最后一个字节25为电压值，转为10进制除非10即3.7V
            int batteryValue = Integer.parseInt(b5ToHex, 16);
            infraredinduction.setVoltage(new BigDecimal(batteryValue).divide(new BigDecimal("10")));
            equipmentData.setEntity(infraredinduction);
        }
        return equipmentData;
    }

    @Override
    public Infraredinduction save(Infraredinduction equipment, boolean alarmFlag) {
        equipment.setAlarmFlag(alarmFlag);
        infraredinductionService.save(equipment);
        return equipment;
    }

    /*public static void main(String[] args) {
        byte[] sources = Base64Utils.decodeFromString("AgEBQCU=");
        String b1ToHex = Strings.padStart(Integer.toHexString(sources[0] & 0xff), 2, '0');
        String b2ToHex = Strings.padStart(Integer.toHexString(sources[1] & 0xff), 2, '0');
        String b3ToHex = Strings.padStart(Integer.toHexString(sources[2] & 0xff), 2, '0');
        String b4ToHex = Strings.padStart(Integer.toHexString(sources[3] & 0xff), 2, '0');
        String b5ToHex = Strings.padStart(Integer.toHexString(sources[4] & 0xff), 2, '0');
        if("02".equals(b1ToHex) && "01".equals(b2ToHex) && "01".equals(b3ToHex)) {
            String busiBinary = Strings.padStart(Integer.toBinaryString(sources[3] & 0xff), 8, '0');
            String sensorStatus = busiBinary.substring(0, 2);
            String batteryStatus = busiBinary.substring(2, 4);
            String tamperStatus = busiBinary.substring(4, 5);
            String keyStatus = busiBinary.substring(5, 8);
            // bit7-6传感器状态 00代表正常，01代表告警，10代表故障，11代表预热
            // bit5-4电池状态 00代表正常，01代表电池低电量；
            // bit3防拆状态，0代表正常，1代表被拆；
            // bit2-0按键状态(暂未实现)，000代表正常，001代表按键自检；
            // 第5个字节是电压值,电池电压上报值为实际电池电压的10倍，比如电池电压为3.6V，则上报值为0x24
            Infraredinduction infraredinduction = new Infraredinduction();
            infraredinduction.setDatastring("");
            infraredinduction.setState(sensorStatus);
            infraredinduction.setBatterystate(batteryStatus);
            infraredinduction.setTamperstate(tamperStatus);
            infraredinduction.setKeystate(keyStatus);
            // 第5个字节是电压值,电池电压上报值为实际电池电压的10倍，比如电池电压为3.6V，则上报值为0x24
            // 最后一个字节25为电压值，转为10进制除非10即3.7V
            int batteryValue = Integer.parseInt(b5ToHex, 16);
            infraredinduction.setVoltage(new BigDecimal(batteryValue / 10));
        }
        System.out.println("");
    }*/

}
